{% extends is_admin ? "backend/index.html.twig" : "default/base.html.twig" %}

{% block head %}
    {% block stylesheets %}
        {{ parent() }}
        {% for file in css_files %}
            <link type="text/css" rel="stylesheet" href={{ file }}></script>
        {% endfor %}
        <link type="text/css" rel="stylesheet" href="{{ base_url() }}css/jasny-bootstrap.min.css"></script> {# usado para dar estilo a botones para subir logo #}
    {% endblock %}
    {% block javascripts %}
        {{ parent() }}
        <script src="{{ base_url() }}js/jasny-bootstrap.min.js"></script> {# usado para dar estilo a botones para subir logo #}
        {% for file in js_files %}
            <script src={{ file }}></script>
        {% endfor %}
        <script type="text/javascript"> {# usado para subir logo por ajax #}
            $(document).on('submit', '.form_logo', function(event) {
                    event.preventDefault(); //Prevent Default action. 
                    var form = $(this);
                    console.debug('form: '+form+'');
                    var id_complejo = form.attr('action');
                    console.debug('id complejo: '+id_complejo+'');
                    var message_box = form.find('.message-box');
                    var file_input = form.find('.fileinput');
                    var datos = new FormData(form[0]);
                    console.debug('datos: '+datos+'');
                    $.ajax({
                        type: 'POST',
			data : datos,
                        mimeType: 'multipart/form-data',
                        contentType: false,
                        cache: false,
                        processData: false,
                        url: '{{ base_url('auth/upload_logo') }}'+'/'+id_complejo+'',
                        beforeSend: function(resp){
                            console.debug('{{ base_url('auth/upload_logo') }}'+'/'+id_complejo+'');
                        },
                        success: function(resp) {
                            //$(this+'.message-box').append(resp);
                            console.debug('success :'+resp);
                            message_box.html(resp);
                            file_input.fileinput('reset');
                            
                        },
                        error: function(resp) {
                            console.debug('error de la concha de la lora:'+resp);
                            message_box.html(resp);
                        }
                    });
                });
        </script>
    {% endblock %}
{% endblock %}
{% block body %}
    {{ parent() }} 
    {% if is_admin %} {# si es admin utilizo clases para visualizar correctamente el menu de administrador #} 
        <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
            <h1 class="page-header">Panel Administrador</h1>
            <h2 class="sub-header">{{ title }}</h2>
        {% if message is empty == false %}<h2 class="sub-header"><div class="alert alert-info">{{ message }}</div></h2>{% endif %}
    {% else %} {# si no es admin no se muestra el menu del aministrador y se muestra la tabla en toda la pantalla #} 
        <div class="container">
            <h1 class="page-header">{{ title }}</h1>
    {% endif %}
        <div class="table-responsive">
            <table class="table table-striped">
                <thead>
                    <tr>
                        <th>Complejo</th>
                        <th></th>
                        <th>Localidad</th>
                        <th>Teléfono</th>
                            {% if is_admin %}
                            <th>Grupo/s</th>
                            <th>Estado</th>
                            <th>Acción</th>
                            {% endif %}
                    </tr>
                </thead>
                <tbody>
                    {% for user in list_users %}
                        <tr>
                            <td>
                                {% if user.logo is null %} {# si no tiene logo cargado muestro uno por default #} 
                                    <img id="image_user_{{ user.id }}" src="{{ base_url('img/logos/default_logo.png') }}" alt="{{ user.first_name }}" class="img-thumbnail" />
                                {% else %}
                                    <img id="image_user_{{ user.id }}" src="{{ base_url('img/logos/'~user.logo) }}" alt="{{ user.first_name }}" class="img-thumbnail"/>
                                {% endif %}
                                {% if is_admin %} {# si es admin muestro el boton para cambiar el logo #} 
                                <form class="form_logo" action="{{ user.id }}" method="post" accept-charset="utf-8" enctype="multipart/form-data">
                                    <div class="fileinput fileinput-new btn-group" data-provides="fileinput">
                                        <span class="btn btn-default btn-file btn-xs"><span class="fileinput-new">&nbsp;&nbsp;Cambiar&nbsp;&nbsp;</span><span class="fileinput-exists">&nbsp;&nbsp;Cambiar&nbsp;&nbsp;</span><input type="file" name="logo"></span>
                                        <span class="fileinput-filename"></span>
                                        <a href="#" class="close fileinput-exists" data-dismiss="fileinput" style="float: none">&times;</a>
                                        <input class="submit_logo btn btn-default btn-xs" type="submit" name="submit_logo" value="Guardar">
                                    </div>
                                    <div class="message-box"></div>
                                </form>
                                {% endif %}                                      
                            </td>
                            <td>{{ user.first_name }}</td>
                            <td>{{ user.location }}</td>
                            <td>{{ user.phone }}</td>
                            {% if is_login and is_admin %}
                                <td>
                                    {% for group in user.groups %}
                                        {{ group.name }}
                                    {% endfor %}
                                </td>
                                <td>
                                    {% if user.active %}
                                        <a href={{  base_url('auth/deactivate/'~user.id) }}>Desactivar</a>
                                    {% else %}
                                        <a href={{ base_url('auth/activate/'~user.id) }}>Activar</a>
                                    {% endif %}
                                </td>
                                <td>
                                    <a href={{ base_url('auth/edit_user/'~user.id) }} >Editar</a>
                                </td>
                            {% endif %}
                        </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
{% endblock %}
